//
//  UIButton+Extension.swift
//  TodayNews
//
//  Created by monkey on 2020/2/3.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit

extension UIButton
{
    /// 便利构造方法
    /// - Parameters:
    ///   - title: 标题
    ///   - selectedTitle: 选中状态标题
    ///   - fontSize: 字体大小
    ///   - normalColor: 默认字体颜色
    ///   - highlightedColor: 高亮字体颜色
    ///   - imageName: 图片名
    ///   - highlightedImageName: 高亮图片名
    ///   - backgroundColor: 背景色
    ///   - backgroundImageName: 背景图片名
    ///   - target: 监听者
    ///   - action: 监听方法
    public convenience init(title: String? = nil,
                            selectedTitle: String? = nil,
                            fontSize: CGFloat = 15.0,
                            normalColor: UIColor? = nil,
                            highlightedColor: UIColor? = nil,
                            imageName: String? = nil,
                            highlightedImageName: String? = nil,
                            backgroundColor: UIColor? = nil,
                            backgroundImageName: String? = nil,
                            target: AnyObject? = nil,
                            action: Selector? = nil)
    {
        self.init(type: .custom)
        
        if title != nil {
            setTitle(title, for: .normal)
            setTitle(selectedTitle, for: .selected)
            setTitleColor(normalColor, for: .normal)
            setTitleColor(highlightedColor, for: .highlighted)
            titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        }
        
        if imageName != nil {
            setImage(UIImage(named: imageName!), for: .normal)
        }
        
        if highlightedImageName != nil {
            setImage(UIImage(named: highlightedImageName!), for: .highlighted)
        }
        
        if backgroundColor != nil {
            self.backgroundColor = backgroundColor
        }
        
        if backgroundImageName != nil {
            setBackgroundImage(UIImage(named: backgroundImageName!), for: .normal)
        }
        
        if target != nil && action != nil {
            addTarget(target, action: action!, for: .touchUpInside)
        }
        
        sizeToFit()
    }
    
    public enum ButtonEdgeInsetsStyle
    {
        // 图片在上，文字在下
        case imageTop
        /// 图片在左，文字在右
        case imageLeft
        /// 图片在下，文字在上
        case imageBottom
        /// 图片在右，文字在左
        case imageRight
    }
        
    /// 调整按钮图片文字位置
    /// - Parameters:
    ///   - style: 样式
    ///   - space: 间距
    public func layoutButtonWithEdgInsetStyle(_ style: UIButton.ButtonEdgeInsetsStyle, space:CGFloat) -> Void
    {
        guard let imageWidth = imageView?.bounds.width,
              let imageHeight = imageView?.bounds.height,
              let labelWidth = titleLabel?.intrinsicContentSize.width,
              let labelHeight = titleLabel?.intrinsicContentSize.height else {
            return
        }
        
        var imageEdgeInsets = UIEdgeInsets.zero
        var labelEdgeInsets = UIEdgeInsets.zero
        switch style {
        case .imageTop:
            imageEdgeInsets = UIEdgeInsets(top: -labelHeight - space * 0.5, left: 0, bottom: 0, right: -labelWidth)
            labelEdgeInsets = UIEdgeInsets(top: 0, left: -imageWidth, bottom: -imageHeight - space * 0.5, right: 0)
        case .imageLeft:
            imageEdgeInsets = UIEdgeInsets(top: 0, left: -space * 0.5, bottom: 0, right: space * 0.5)
            labelEdgeInsets = UIEdgeInsets(top: 0, left: space * 0.5, bottom: 0, right: -space * 0.5)
        case .imageRight:
            imageEdgeInsets = UIEdgeInsets(top: 0, left: labelWidth + space * 0.5, bottom: 0, right:  -labelWidth - space * 0.5)
            labelEdgeInsets = UIEdgeInsets(top: 0, left: -imageWidth - space * 0.5, bottom: 0, right: imageWidth + space * 0.5)
        case .imageBottom:
            imageEdgeInsets = UIEdgeInsets(top: 0,left: 0,bottom: -labelHeight-space/2.0,right: -labelWidth)
            labelEdgeInsets = UIEdgeInsets(top: -imageHeight - space * 0.5, left: -imageWidth, bottom: 0, right: 0)
        }
        
        self.imageEdgeInsets = imageEdgeInsets
        self.titleEdgeInsets = labelEdgeInsets
    }
}

